package com.haiyou.dc.oa.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.dc.oa.entity.Person;
import com.haiyou.dc.oa.entity.dto.PersonDTO;
import com.haiyou.dc.oa.service.PersonService;
import com.haiyou.dc.oa.util.Layui;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;

/**
 * @author root
 * @date 2020-01-05 16:26:23
 */
@Controller
@RequestMapping("/person")
@Slf4j
public class PersonController {

    private static final String prefix = "person";

    @Autowired
    private PersonService personService;


    @ApiOperation(value = "查看数据")
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id") Integer id, Model model) {
        Person person = personService.selectByPrimaryKey(id);
        model.addAttribute("person", person);
        return prefix + "/info";
    }

    @ApiOperation(value = "查看数据")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        Person person = personService.selectByPrimaryKey(id);
        model.addAttribute("person", person);
        return prefix + "/edit";
    }

    @GetMapping("/list")
    public String list() {
        return prefix + "/list";
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @GetMapping("/index")
    public String index(Model model, HttpServletRequest request) throws InterruptedException {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        model.addAttribute("pageNum", StringUtils.isEmpty(pageNum) ? "1" : pageNum);
        model.addAttribute("pageSize", StringUtils.isEmpty(pageSize) ? "10" : pageSize);
        return prefix + "/index";
    }

    /**
     * -------------------------------------------ajax异步请求-----------------------------------------
     **/

    @ApiOperation(value = "修改数据")
    @PostMapping("/ajaxUpdate")
    @ResponseBody
    public R ajaxUpdate(Person person) {
        log.info("--------------------->接收修改参数：{}", person);
        personService.updateByPrimaryKeySelective(person);
        return R.ok();
    }

    @ApiOperation(value = "保存数据")
    @PostMapping("/ajaxSave")
    @ResponseBody
    public R ajaxSave(Person person) {
        log.info("--------------------->接收保存参数：{}", person);
        personService.insertSelective(person);
        return R.ok().put("url", "/person/list");
    }

    @ApiOperation(value = "删除数据")
    @PostMapping("/ajaxDelete")
    @ResponseBody
    public R ajaxDelete(@RequestParam Integer[] ids) {
        log.info("--------------------->接收删除参数：{}", ids);
        for (Integer id : ids) {
            personService.deleteByPrimaryKey(id);
        }
        return R.ok();
    }

    @ApiOperation(value = "查看列表数据")
    @PostMapping("/list")
    @ResponseBody
    public R list(PersonDTO personDTO) {
        log.info("--------------------->接收查询参数：{}", personDTO);
        Example example = new Example(Person.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        if (personDTO.getId() != null) {
            criteria.andEqualTo("id", personDTO.getId());
        }
        if (!StringUtils.isEmpty(personDTO.getPersonBirthDay())) {
            criteria.andLike("personBirthDay", "%" + personDTO.getPersonBirthDay() + "%");
        }
        if (!StringUtils.isEmpty(personDTO.getPersonName())) {
            criteria.andLike("personName", "%" + personDTO.getPersonName() + "%");
        }
        Page<Person> page = PageHelper.startPage(personDTO.getPageNum(), personDTO.getPageSize(), true, false, false);
        personService.selectByExample(example);
        PageInfo<Person> info = new PageInfo<>(page.getResult());
        return Layui.ok(info);
    }


}